<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">学生管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>学生管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="student-search-key">
                <option value="">-请选择-</option>
                <option value="user_id">ID</option>
                <option value="username">账号</option>
                <option value="nick_name">用户名</option>
                <option value="phone">手机号</option>
                <option value="email">邮箱</option>
                <option value="gpname">小组名</option>
            </select>&emsp;
            <input id="student-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="student-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="student-table" lay-filter="student-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="student-table-bar">
    <a class="layui-btn  layui-btn-xs" lay-event="editGroup">分配小组</a>
    <a class="layui-btn  layui-btn-xs" lay-event="editCoach">分配教练</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin', 'index'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var index = layui.index;


        index.getUser(function (user) {
            teacher_name=user
        })


        // 渲染表格
        table.render({
            elem: '#student-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-user/students',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token,
                teacher_name:teacher_name.username
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '用户编号'},
                {field: 'username', sort: true, title: '账号'},
                {field: 'nickname', sort: true, title: '用户名'},
                {field: 'phone', sort: true, title: '手机号'},
                {field: 'email', sort: true, title: '邮箱'},
                {
                    sort: true, title: '所在小组', templet: function (d) {
                        return d.gpname == "" || d.gpname == null ? "暂无小组" : d.gpname
                    }
                },
                {
                    sort: true, title: '性别', templet: function (d) {
                        return d.sex == 0 ? "男" : "女"
                    }
                },
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                // {field: 'enabled', sort: true, templet: '#user-tpl-state', title: '状态',width: 150},
                {align: 'center', toolbar: '#student-table-bar', title: '操作', width: 150}
            ]]
        });

        // 添加按钮点击事件
        $('#student-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(student-table)', function (obj) {
            var data = obj.data;
            showEditModel(data);
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            //修改sex值
            if (data) {
                data.sex = data.sex + '';
            }
            admin.putTempData('t_student', data);
            var title = data ? '分配小组' : '添加学生';
            admin.popupCenter({
                title: title,
                path: 'pages/system/student_form.html',
                finish: function () {
                    table.reload('-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#student-btn-search').click(function () {
            var key = $('#student-search-key').val();
            var value = $('#student-search-value').val();
            console.log("adsgdjhd")
            table.reload('student-table', {where: {searchKey: key, searchValue: value}});
        });

    });
</script>